package com.lnu.information.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author : jian.zhang-2
 * @description:
 * @date : Create in 4:08 PM 2022/5/19
 * @modified By :
 */
public class ExampleFrame_05 extends JFrame {
    public static void main(String[] args) {
        new ExampleFrame_05();
    }

    public ExampleFrame_05() throws HeadlessException {
        this.setTitle("创建菜单栏");
        this.setBounds(100,100,500,375);
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        JMenu menu = new JMenu("菜单名称(F)");
        menu.setMnemonic('F');
        menuBar.add(menu);

        JMenuItem sonMenuItem = new JMenuItem("子菜单项名称");
        sonMenuItem.addActionListener(new ItemListener());
        menu.add(sonMenuItem);
        menu.addSeparator();
        JMenuItem exit = new JMenuItem("退出");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        menu.add(exit);
        JMenu menu2 = new JMenu("菜单名称2");
        JMenuItem menuItem2 = new JMenuItem("菜单项名称2");
        menuItem2.addActionListener(new ItemListener());
        menu.add(menuItem2);
        menu2.add(menuItem2);
        this.setVisible(true);
    }

    private class ItemListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            JMenuItem menuItem = (JMenuItem)e.getSource();
            System.out.println("您点击的菜单项 " + menuItem.getText());
        }
    }

}
